package com.lagou.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {

    public static void main(String[] args) {
        System.out.println("dd");
    }

    //创建目录
    @Test
    public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
        Configuration configuration = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"),configuration,"root");
        fs.mkdirs(new Path("/test1"));
        fs.close();
    }

    //上传
    @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        //configuration.set("dfs.replication", "1");
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"), configuration, "root");
        // 2 上传文件
        fs.copyFromLocalFile(new Path("d:/lagou.txt"), new Path("/lagou.txt"));
        // 3 关闭资源
        fs.close();
        System.out.println("end");
    }

    //下载
    @Test
    public void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"),configuration,"root");

        fs.copyToLocalFile(false,new Path("/lagou.txt"),new Path("d:/lagou_cy.txt"),true);

        fs.close();
    }

    //删除文件
    @Test
    public void testDelete() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://linux121:9000"), configuration, "root");

        fs.delete(new Path("/lagou.txt"),true);
        fs.close();
    }
}
